const app = getApp();
var utils = require("../../utils/utils.js");
var server = require("../../utils/server.js");
var config = require("../../config.js");
var apis = require("../../utils/api.js");

Page({
  data: {
    pageSize: -1,
    pageNo: 1,
    loading: false,
    nodata: false,
    authStatus: app.globalData.userInfo.authStatus,
    photoAddress: '',
    sortingPortNumber: '',
    deliveryNoteId: '',
    dataList: [],
    image_base64: ''

  },

  onLoad(options) {
    if (!options.value) {
      return
    }
    let params = JSON.parse(options.value);
    console.log(params)
    this.setData({
      photoAddress: params.photoAddress,
      sortingPortNumber: params.sortingPortNumber,
      batchNumber: params.batchNumber,
      entCustomerId: params.entCustomerId,
      deliveryNoteId: params.id
    })
    if (params.photoAddress) {
      this.imageFind()
    }
    this.pageMaterialInfo()
  },


  processString(inputString) {
    // 1. 替换 "<view>" 为空字符串 (使用正则表达式)
    const stringWithoutViewStart = inputString.replace(/<view>/g, "");

    // 2. 替换 "</view>" 为逗号 (使用正则表达式)
    const stringWithComma = stringWithoutViewStart.replace(/<\/view>/g, ",");

    // 3. 使用逗号拆分为数组
    const stringArray = stringWithComma.split(",");

    // 4. 去除数组中的空字符串 (可选，如果需要)
    const filteredArray = stringArray.filter(item => item !== "");

    return filteredArray;
  },

  pageMaterialInfo() {
    let _this = this;
    var prams = {
      deliveryNoteId: _this.data.deliveryNoteId,
      pageSize: 100,
      pageNo: 1
    }
    var api = apis.pageMaterialInfo;
    server.getRequest(config.xburl, api, prams,
      function (res) {
        let dataList = res.data.list
        _this.setData({
          dataList: dataList
        });
      },
      function (err) {
        utils.showToast(err)
      })
  },

  imageFind() {
    let _this = this;
    let image_path = _this.data.photoAddress
    var params = {
      'image_path': image_path
    };
    utils.showLoading('数据请求中...', )
    wx.request({
      url: config.imageFindUrl,
      method: 'post',
      data: params,
      timeout: 20000,
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res)
        let image_base64 = res.data.image_base64
        _this.setData({
          image_base64: `data:image/jpg;base64,${image_base64}`
        })
      },
      fail: function (res) {
        utils.showToast('网络错误');
      },
      complete: function (res) {
        wx.hideLoading()
      }
    })
  },

  previewImage(e) {
    let _this = this
    wx.previewImage({
      current: _this.data.image_base64,
      urls: [_this.data.image_base64]
    });
  },


});